//
//  HDCFBuyInfoViewController.m
//  HaoDeCaiFu
//
//  Created by admin on 2017/12/22.
//  Copyright © 2017年 郑开宏. All rights reserved.
//

#import "HDCFBuyInfoViewController.h"
#import "YHVerifyPhoneNum.h"//验证手机号

@interface HDCFBuyInfoViewController ()<UITextFieldDelegate>
{
    UIButton * fundOrderBut;//提交预约按钮
    UIView * headerView;
    
    UITextField * userNameField;//用户名字的文本框
    UITextField * passwordField;//电话的文本框
    UITextField * amountField;//用户额度的文本框
    
    NSString * nameString;
    NSString * phoneString;
    NSString * amountString;
}
@end

@implementation HDCFBuyInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGB(240, 240, 240);
    self.title=@"购买信息填写";
    
    [self initView];
    [self initInputInfoView];
    [self createBottomView];
    // Do any additional setup after loading the view.
}
-(void)initView
{
    headerView = [[UIView alloc]init];
    headerView.frame = CGRectMake(0, 0, screenWidth, 89);
    headerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:headerView];
    
    UILabel * titleLabel = [UILabel new];
    titleLabel.frame = CGRectMake(19, 20, screenWidth - 100, 19);
    titleLabel.text = @"环球资产配置投资组合";
    titleLabel.textColor = RGB(32, 32, 32);
    titleLabel.font = [UIFont systemFontOfSize:19.0];
    [headerView addSubview:titleLabel];
    
    UILabel * typeLabel = [UILabel new];
    typeLabel.text = @"私募基金";
    typeLabel.frame = CGRectMake(19, titleLabel.bottom+13,[self calculateWidth:typeLabel.text], 19);
    typeLabel.textColor = RGB(170, 170, 170);
    typeLabel.font = [UIFont systemFontOfSize:15.0];
    [headerView addSubview:typeLabel];
    
    UILabel * fundCodeLabel = [UILabel new];
    fundCodeLabel.text = @"A1001";
    fundCodeLabel.frame = CGRectMake(typeLabel.right+10, titleLabel.bottom+13,[self calculateWidth:fundCodeLabel.text], 19);
    fundCodeLabel.textColor = RGB(170, 170, 170);
    fundCodeLabel.font = [UIFont systemFontOfSize:15.0];
    [headerView addSubview:fundCodeLabel];
    
}
-(void)initInputInfoView
{
    
    UIView * contentView = [UIView new];
    contentView.frame = CGRectMake(0, headerView.bottom + 10, screenWidth, 185);
    contentView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:contentView];
    
    
    userNameField = [[UITextField alloc]init];
    userNameField.frame = CGRectMake(19, 10, screenWidth - 19*2, 40);
    userNameField.placeholder = @"姓名";
    userNameField.delegate = self;
    //首字母是否大写
    userNameField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    userNameField.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
    userNameField.textColor = RGB(170, 170, 170);
    //输入框中是否有个叉号，在什么时候显示，用于一次性删除输入框中的内容
    userNameField.clearButtonMode = UITextFieldViewModeAlways;
    [contentView addSubview:userNameField];
    UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_name"]];
    image.bounds = CGRectMake(0, 0, 20, 20);
    userNameField.leftView=image;
    userNameField.leftViewMode = UITextFieldViewModeAlways;
    
    
    UILabel * oneLabel = [[UILabel alloc]init];
    oneLabel.frame = CGRectMake(19, userNameField.bottom-1, screenWidth - 19*2, 0.5);
    oneLabel.backgroundColor = RGB(240, 240, 240);
    [contentView addSubview:oneLabel];
    
    passwordField = [[UITextField alloc]init];
    passwordField.frame = CGRectMake(19, oneLabel.bottom+20, screenWidth - 19*2, 40);
    passwordField.placeholder = @"电话";
    //每输入一个字符就变成点 用语密码输入
    //passwordField.secureTextEntry = YES;
    //再次编辑就清空
    passwordField.clearsOnBeginEditing = YES;
    passwordField.delegate = self;
    passwordField.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
    passwordField.textColor = RGB(170, 170, 170);
    //输入框中是否有个叉号，在什么时候显示，用于一次性删除输入框中的内容
    passwordField.clearButtonMode = UITextFieldViewModeAlways;
    passwordField.keyboardType = UIKeyboardTypePhonePad;
    [contentView addSubview:passwordField];
    UIImageView *images=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_yuliushouji"]];
    images.bounds = CGRectMake(0, 0, 20, 20);
    passwordField.leftView=images;
    passwordField.leftViewMode = UITextFieldViewModeAlways;
    
    UILabel * twoLabel = [[UILabel alloc]init];
    twoLabel.frame = CGRectMake(19, passwordField.bottom -1, screenWidth - 19*2, 0.5);
    twoLabel.backgroundColor = RGB(240, 240, 240);
    [contentView addSubview:twoLabel];
    
    
    
    amountField = [[UITextField alloc]init];
    amountField.frame = CGRectMake(19, twoLabel.bottom+20, screenWidth - 19*2, 40);
    amountField.placeholder = @"额度";
    //每输入一个字符就变成点 用语密码输入
    //amountField.secureTextEntry = YES;
    //再次编辑就清空
    amountField.clearsOnBeginEditing = YES;
    amountField.delegate = self;
    amountField.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
    amountField.textColor = RGB(170, 170, 170);
    //输入框中是否有个叉号，在什么时候显示，用于一次性删除输入框中的内容
    amountField.clearButtonMode = UITextFieldViewModeAlways;
    amountField.keyboardType = UIKeyboardTypeDecimalPad;
    [contentView addSubview:amountField];
    UIImageView *imagesThree=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_edu"]];
    imagesThree.bounds = CGRectMake(0, 0, 20, 20);
    amountField.leftView=imagesThree;
    amountField.leftViewMode = UITextFieldViewModeAlways;
    
    UILabel * threeLabel = [[UILabel alloc]init];
    threeLabel.frame = CGRectMake(19, amountField.bottom -1, screenWidth - 19*2, 0.5);
    threeLabel.backgroundColor = RGB(240, 240, 240);
    [contentView addSubview:threeLabel];
}
-(void)createBottomView
{
    fundOrderBut = [UIButton buttonWithType:UIButtonTypeCustom];
    fundOrderBut.frame = CGRectMake(0, screenHeight - 49-KNavHeight , screenWidth, 49);
    [fundOrderBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [fundOrderBut setTitle:@"提交" forState:UIControlStateNormal];
    [fundOrderBut addTarget:self action:@selector(commitOrder) forControlEvents:UIControlEventTouchUpInside];
    [fundOrderBut setBackgroundColor:ZKcolor(184, 27, 36)];
    [self.view addSubview:fundOrderBut];
}
//点击开始预约按钮
-(void)commitOrder
{
    [userNameField resignFirstResponder];
    [passwordField resignFirstResponder];
    [amountField resignFirstResponder];
    if (nameString.length <= 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入姓名"];
        [SVProgressHUD dismissWithDelay:1];
        return;
    }
    
    if(phoneString.length <= 0)
    {
        [SVProgressHUD showInfoWithStatus:@"请输入电话"];
        [SVProgressHUD dismissWithDelay:1];
        return;
    }
    else
    {
        //校验手机号
        if (![YHVerifyPhoneNum isMobileNumber:phoneString]) {
            [SVProgressHUD showInfoWithStatus:@"请输入正确的电话"];
            [SVProgressHUD dismissWithDelay:1];
            return;
        }
    }
    
    if (amountString.length <= 0)
    {
        [SVProgressHUD showInfoWithStatus:@"请输入额度"];
        [SVProgressHUD dismissWithDelay:1];
        return;
    }

    [SVProgressHUD show];
    NSString * urlStr = @"api/Product";
    NSDictionary *paramet = @{@"ProductId":[NSString stringWithFormat:@"%ld",(long)_productId],@"Name":nameString,@"Phone":phoneString,@"Amount":amountString};
    NSString *jsonStr=[self dictionaryToJson:paramet];
    [HttpRequest sendSelfPostRequestWithURL:urlStr WithParamets:jsonStr WithParamet:nil success:^(id result) {
        [SVProgressHUD dismiss];
        
        if ([result[@"code"]integerValue] == 1) {
            [SVProgressHUD showSuccessWithStatus:@"预约成功"];
            [SVProgressHUD dismissWithDelay:1];
            [self.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            [SVProgressHUD showInfoWithStatus:[NSString stringWithFormat:@"%@",result[@"msg"]]];
            [SVProgressHUD dismissWithDelay:1];
        }
        //LLLog(@"%@",result);
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}
#pragma mark -
#pragma mark UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField == userNameField) {
        nameString = textField.text;
    }
    else if (textField == passwordField)
    {
        phoneString = textField.text;
    }
    else if (textField == amountField)
    {
        amountString = textField.text;
    }
}
#pragma mark -
#pragma mark 计算字符串长度
- (CGFloat)calculateWidth:(NSString *)str {
    NSDictionary *dic  = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
    CGSize size = [str sizeWithAttributes:dic];
    return size.width;
}
//字典转json
- (NSString *)dictionaryToJson:(NSDictionary *)dic{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString=[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSString *jsonTemp = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    return jsonTemp;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
